/**
 * 严肃声明：
 * 开源版本请务必保留此注释头信息，若删除我方将保留所有法律责任追究！
 * 本软件已申请软件著作权，受国家版权局知识产权以及国家计算机软件著作权保护！
 * 可正常分享和学习源码，不得用于违法犯罪活动，违者必究！
 * Copyright (c) 2019-2021 十三 all rights reserved.
 * 版权所有，侵权必究！
 */
package ltd.newbee.mall.api.mall;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import ltd.newbee.mall.api.mall.param.*;
import ltd.newbee.mall.api.mall.vo.NewBeeMallOrderDetailVO;
import ltd.newbee.mall.api.mall.vo.NewBeeMallUserAddressVO;
import ltd.newbee.mall.api.mall.vo.RspObjBean;
import ltd.newbee.mall.common.ServiceResultEnum;
import ltd.newbee.mall.config.annotation.TokenToAdminUser;
import ltd.newbee.mall.config.annotation.TokenToMallUser;
import ltd.newbee.mall.config.constant.Log;
import ltd.newbee.mall.config.constant.PreventDuplicateSubmit;
import ltd.newbee.mall.entity.*;
import ltd.newbee.mall.service.AccessTokenService;
import ltd.newbee.mall.service.NewBeeMallGoodsService;
import ltd.newbee.mall.service.NewBeeMallOrderService;
import ltd.newbee.mall.service.NewBeeMallUserAddressService;

import ltd.newbee.mall.service.impl.PublicPay;
import ltd.newbee.mall.service.impl.SfRouteQueryService;
import ltd.newbee.mall.util.Base64Utils;
import ltd.newbee.mall.util.Result;
import ltd.newbee.mall.util.ResultGenerator;
import ltd.newbee.mall.util.Util;
import nuonuo.open.sdk.NNOpenSDK;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Iterator;
import java.util.UUID;

@RestController
@Api(value = "v1", tags = "微信公众号支付")
@RequestMapping("/api/v1")
@Slf4j
public class WxPayAPI {

    @Resource
    private NewBeeMallUserAddressService mallUserAddressService;

    @Resource
    private NewBeeMallGoodsService newBeeMallGoodsService;

    @Resource
    private NewBeeMallOrderService newBeeMallOrderService;

    @Resource
    private PublicPay publicPay;


    @Autowired
    private AccessTokenService accessTokenService;
    @Value("${bill.appKey}")
    public String appKey;
    @Value("${bill.appSecret}")
    public String appSecret;
    @Value("${bill.url}")
    public String url;
    @Value("${bill.taxnum}")
    public String taxnum;




    @PostMapping("/pay")
    @ApiOperation(value = "支付下单", notes = "")
    @PreventDuplicateSubmit(key = "order_submit", expire = 10, message = "订单正在处理中，请勿重复提交")
    @Log(module = "支付下单", action = "支付下单")
    public Result<Boolean> pay(@RequestBody WxPayParam wxPayParam, @TokenToMallUser MallUser loginMallUser) {
        log.info("getOrderNo=" + wxPayParam.getOrderNo() + ",wxPayParam.getPayType=" + wxPayParam.getPayType());
        NewBeeMallOrderDetailVO newBeeMallOrderDetailVO = newBeeMallOrderService.getOrderDetailByOrderNo(wxPayParam.getOrderNo(), loginMallUser.getUserId());
        log.info("newBeeMallOrderDetailVO=" + JSONObject.toJSONString(newBeeMallOrderDetailVO));
        String merOrderId = "103A" + Util.getMerchantOrderId();

        //支付之前先修改主订单添加支付订单号
        NewBeeMallOrder newBeeMallOrder = new NewBeeMallOrder();
        newBeeMallOrder.setOrderId(newBeeMallOrderDetailVO.getOrderId());
        newBeeMallOrder.setPayNo(merOrderId);
        newBeeMallOrderService.updateByPrimaryKeySelective(newBeeMallOrder);

        String payUrl = "";
        try {
            payUrl = publicPay.pay(merOrderId, wxPayParam, newBeeMallOrderDetailVO);
        } catch (Exception e) {
            return ResultGenerator.genFailResult(e.getMessage());
        }
        return ResultGenerator.genSuccessResult(payUrl);
    }


    @PostMapping("/applyRefund")
    @ApiOperation(value = "申请退款", notes = "")
    @PreventDuplicateSubmit(key = "applyRefund_submit", expire = 10, message = "订单正在处理中，请勿重复提交")
    @Log(module = "申请退款", action = "申请退款")
    public Result<Boolean> applyRefund(@RequestBody RefundApplyParam refundApplyParam, @TokenToMallUser MallUser loginMallUser) {
        return   newBeeMallOrderService.applyRefund(refundApplyParam);
    }

    @PostMapping("/applyInvoice")
    @ApiOperation(value = "申请开票", notes = "")
    @PreventDuplicateSubmit(key = "applyInvoice_submit", expire = 10, message = "订单正在处理中，请勿重复提交")
    @Log(module = "申请开票", action = "申请开票")
    public Result<Boolean> applyInvoice(@RequestBody InvoiceApplyParam invoiceApplyParam, @TokenToMallUser MallUser loginMallUser) {
        return   newBeeMallOrderService.applyInvoice(invoiceApplyParam);
    }




    @PostMapping("/newBill")
    @ApiOperation(value = "开具发票", notes = "")
    @PreventDuplicateSubmit(key = "newBill_submit", expire = 10, message = "订单正在处理中，请勿重复提交")
    @Log(module = "开具发票", action = "开具发票")
    public Result<Boolean> newBill(@RequestBody BillInputParam billInputParam, @TokenToMallUser MallUser loginMallUser) {
        return   newBeeMallOrderService.newBill(billInputParam);
    }

    @PostMapping("/selectBill")
    @ApiOperation(value = "查询发票", notes = "")
    @Log(module = "查询发票", action = "查询发票")
    public Result selectBill(@RequestBody BillInputParam billInputParam, @TokenToMallUser MallUser loginMallUser) {
        return newBeeMallOrderService.selectBill(billInputParam.getOrderNo());
    }






}
